#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"subsetsum.h"

int main(int argc,char * argv[]){

    if(argc < 2){

        printf("need input data filename in command.\n");
        return EXIT_FAILURE;
    }
    FILE * fptr = fopen(argv[1],"r");
    if(NULL == fptr){

        printf("open file failed,filename = %s.\n",argv[1]);
        return EXIT_FAILURE;
    }
    int numInt = countInteger(fptr);

    fseek(fptr,0,SEEK_SET);
    int keyValue;
    if(fscanf(fptr,"%d\n",&keyValue)!=1){

        printf("fscanf error.\n");
        fclose(fptr);
        return EXIT_FAILURE;
    }
    numInt--;   // -去除keyValue
    int * array = malloc(sizeof(int) * numInt);
    int index = 0;
    for(index = 0;index < numInt;index++){
        int aval;
        if(fscanf(fptr,"%d ",&aval)!=1){

            printf("fscanf error.\n");
            fclose(fptr);
            free(array);
            return EXIT_FAILURE;
        }
        array[index] = aval;
    }
    fclose(fptr);
    if(isValidSet(array,numInt) == 1){

        printf("There are %d subsetsums whose sums are %d.\n",subsetSum(array,numInt,keyValue),keyValue);
    }else{

        printf("Invalid set.\n");
    }
    free(array);
    return EXIT_SUCCESS;
}